/*!
    This file is part of KapiSkript.

    Copyright (C) 2009 RaNaN

    KapiSkript is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
function kl_main(event){
//start = new Date();
    var doc = event.originalTarget;
    docloc = doc.location.href;
    if (kl_isKapilandDomain(doc.location)) {

if (kl_prefManager.getIntPref("extensions.ks.sortierung") != 0) {

	var mLinks = kl_eval(doc,"//a[contains(@href, 'page=markt2') and not(contains(@href, 'sort=')) and not(contains(@href, 'welche='))]");
           // var mLinks = FindeTags(doc, "A", new Function("Kn", "return (Kn.getAttribute('href').search('page=markt2') > -1 && (Kn.getAttribute('href').search('sort=') == -1 && Kn.getAttribute('href').search('welche=') == -1));"));


            for (i = 0; i < mLinks.length; i++) {
                if (kl_prefManager.getIntPref("extensions.ks.sortierung") == 1) {
                    sortString = "&welche=2&sort=";
                } else sortString = "&welche=3&sort=";

                mLinks[i].setAttribute("href", mLinks[i].getAttribute("href") + sortString);
            }
        }

//noinspection JSUnusedLocalSymbols
        try{
if(kl_isWelt5(doc.location)){

var head = kl_eval(doc,"//table[@width=920]//td[@width=150]")[0];

var div = NeuesElement(doc,"div","","style","width: 144px; position: relative; color: white; background-color: #1D51B7;padding: 5px;");
 var logoTable = NeuesElement(doc, "table", "");
                var logoTr = NeuesElement(doc, "tr", "");

                var logoTd = doc.createElement("TD");
                var versionTd = doc.createElement("TD");

                var v_1 = NeuesElement(doc, "b", "");
                var v_2 = NeuesElement(doc, "font", "", "face", "Tahoma","color","white");
                var v_3 = doc.createTextNode("KapiSkript " + kl_version);

                logoTd.appendChild(NeuesElement(doc, "img", "", "src", "chrome://kapiland/skin/ks.png", "width", "25px", "height", "25px"));

                logoTd.addEventListener("click", function() {
                    window.open("chrome://kapiland/content/pref.xul", "findfile", "chrome");
                }, true);

                v_2.appendChild(v_3);
                v_1.appendChild(v_2);
                versionTd.appendChild(v_1);

                logoTr.appendChild(logoTd);
                logoTr.appendChild(versionTd);

                logoTable.appendChild(logoTr);

		if(kl_prefManager.getBoolPref("extensions.ks.quicklinks")){

		logoTable.appendChild(NeuesElement(doc,"TR",""));
		logoTable.lastChild.appendChild(NeuesElement(doc,"TD","<a style='padding-right: 5px;' href='http://" + doc.location.host + doc.location.pathname + "?" + "page=schaufenster&UIN=" + extract(doc.body.innerHTML, "UIN=", /["]/)  +"'>"+loca[lang]["sfenster"]+"</a><a href='http://" + doc.location.host + doc.location.pathname + "?" + "page=wettbewerb&UIN=" + extract(doc.body.innerHTML, "UIN=", /["]/) +"'>"+loca[lang]["wbw"]+"</a>","colspan","2"));
		//lokalisieren
		}
		div.appendChild(logoTable);
		head.appendChild(div);

		var bargeld = kl_eval(doc,"//table[@width='100%' and contains(@style,'white')]//tr[2]")[0].textContent.match(/[0-9,\.]+/)[0];
                var festkapital = kl_eval(doc,"//table[@width='100%' and contains(@style,'white')]//tr[3]")[0].textContent.match(/[0-9,\.]+/)[0];


                bargeld = bargeld.replace(loca[lang]["tausend"], "");
                festkapital = festkapital.replace(loca[lang]["tausend"], "");

                bargeld = bargeld.replace(loca[lang]["komma"],".");
                festkapital = festkapital.replace(loca[lang]["komma"],".");
                var geldsumme = bargeld * 1 + festkapital * 1;

                geldsumme = Math.round(geldsumme * 100) / 100;

                geldsumme = Math.setSectionPoints(geldsumme);

                if (kl_prefManager.getBoolPref("extensions.ks.geldsumme")) {
			div.appendChild(doc.createTextNode(loca[lang]["geldsumme"] + geldsumme + " "+loca[lang]["c"]));
                }

                if (kl_prefManager.getBoolPref("extensions.ks.firmensuche")) {

                    var suche = NeuesElement(doc, "FORM", "", "method", "post", "action", doc.location.pathname + "?page=suche&UIN=" + extract(doc.body.innerHTML, "UIN=", /["]/), "style", "margin-bottom: 3px;", "id", "kss");

                    var suchInput = NeuesElement(doc, "INPUT", "", "type", "text", "size", "20", "maxlength", "100", "name", "suche", "value",loca[lang]["firmensuche"]);
                    suchInput.addEventListener("focus", empty, true);
                    suche.appendChild(suchInput);
                    div.appendChild(suche);
                }


}else{

		var menue = kl_eval(doc,"//a[contains(@href,'levels')]/..");

//            var menue = FindeTags(doc, "TD", new Function("Kn", "return (Kn.getAttribute('class') == 'white' && (Kn.firstChild.nodeName == 'FONT' || Kn.firstChild.nodeName == 'B') && Kn.parentNode.nodeName == 'TR');"));
            if (menue.length > 0) {

                var v = 0;
                if (menue[0].childNodes[2].nodeName == "IMG") v += 2;
		if (kl_evalNode(doc,menue[0],".//a/text()").length > 0) v += 1;

                var logoTable = NeuesElement(doc, "table", "");
                var logoTr = NeuesElement(doc, "tr", "");

                var logoTd = doc.createElement("TD");
                var versionTd = doc.createElement("TD");

                var v_1 = NeuesElement(doc, "b", "");
                var v_2 = NeuesElement(doc, "font", "", "face", "Tahoma", "color", "darkgreen");
                var v_3 = doc.createTextNode("KapiSkript " + kl_version);

                logoTd.appendChild(NeuesElement(doc, "img", "", "src", "chrome://kapiland/skin/ks.png", "width", "25px", "height", "25px"));

                logoTd.addEventListener("click", function() {
                    window.open("chrome://kapiland/content/pref.xul", "findfile", "chrome");
                }, true);

                v_2.appendChild(v_3);
                v_1.appendChild(v_2);
                versionTd.appendChild(v_1);

                logoTr.appendChild(logoTd);
                logoTr.appendChild(versionTd);

                logoTable.appendChild(logoTr);

		if(kl_prefManager.getBoolPref("extensions.ks.quicklinks")){

		logoTable.appendChild(NeuesElement(doc,"TR",""));
		logoTable.lastChild.appendChild(NeuesElement(doc,"TD","<a style='padding-right: 5px;' href='http://" + doc.location.host + doc.location.pathname + "?" + "page=schaufenster&UIN=" + extract(doc.body.innerHTML, "UIN=", /["]/)  +"'>"+loca[lang]["sfenster"]+"</a><a href='http://" + doc.location.host + doc.location.pathname + "?" + "page=wettbewerb&UIN=" + extract(doc.body.innerHTML, "UIN=", /["]/) +"'>"+loca[lang]["wbw"]+"</a>","colspan","2"));
		//lokalisieren
		}

                EinfuegenHinter(logoTable, menue[0].childNodes[1 + v]);
                menue[0].removeChild(menue[0].childNodes[3 + v]);

                var bargeld = menue[0].textContent.substring(menue[0].textContent.search(loca[lang]["bargeld"]) +loca[lang]["bargeld"].length+2, menue[0].textContent.search(loca[lang]["festkapital"]) - 2);
                var festkapital = menue[0].textContent.substring(menue[0].textContent.search(loca[lang]["festkapital"])+loca[lang]["festkapital"].length + 2, menue[0].textContent.search(loca[lang]["level"]) - 2);


                bargeld = bargeld.replace(loca[lang]["tausend"], "");
                festkapital = festkapital.replace(loca[lang]["tausend"], "");

                bargeld = bargeld.replace(loca[lang]["komma"],".");
                festkapital = festkapital.replace(loca[lang]["komma"],".");
                var geldsumme = bargeld * 1 + festkapital * 1;

                geldsumme = Math.round(geldsumme * 100) / 100;

                geldsumme = Math.setSectionPoints(geldsumme);

                if (kl_prefManager.getBoolPref("extensions.ks.geldsumme")) {

                    EinfuegenHinter(doc.createTextNode(loca[lang]["geldsumme"] + geldsumme + " "+loca[lang]["c"]), menue[0].childNodes[5 + v]);
                    EinfuegenHinter(doc.createElement("BR"), menue[0].childNodes[5 + v]);
                }

                if (kl_prefManager.getBoolPref("extensions.ks.firmensuche")) {

                    var suche = NeuesElement(doc, "FORM", "", "method", "post", "action", doc.location.pathname + "?page=suche&UIN=" + extract(doc.body.innerHTML, "UIN=", /["]/), "style", "margin-bottom: 3px;", "id", "kss");

                    var suchInput = NeuesElement(doc, "INPUT", "", "type", "text", "size", "20", "maxlength", "100", "name", "suche", "value",loca[lang]["firmensuche"]);
                    suchInput.addEventListener("focus", empty, true);
                    suche.appendChild(suchInput);
                    menue[0].appendChild(suche);
                }
}
          }}catch(e){ throw e;}

}}

function kl_mainInit(){
window.addEventListener("DOMContentLoaded", kl_main, true);
}
//loca[lang][""]

kl_mainInit();